// 影像检查数据
export const imagingExamData = {
  patients: [
    {
      patient_id: "PT2025001",
      name: "黎天剑",
      imaging_exams: {
        summary: {
          CT: "共8次检查",
          MRI: "共5次检查",
          Ultrasound: "共15次检查",
          X_ray: "共20次检查"
        },
        recent_exams: [
          {
            exam_type: "右足第一跖趾关节X光检查",
            date: "2024-01-15 09:30",
            doctor: "王医生",
            department: "放射科",
            status: "发现结石"
          },
          {
            exam_type: "右足关节B超检查",
            date: "2024-01-13 10:15",
            doctor: "张医生",
            department: "超声科",
            status: "正常"
          }
        ],
        status_counts: {
          completed: 2,
          pending: 0,
          abnormal: 1
        }
      }
    },
    {
      patient_id: "PT2025002",
      name: "纪建源",
      imaging_exams: {
        summary: {
          CT: "共7次检查",
          MRI: "共4次检查",
          Ultrasound: "共12次检查",
          X_ray: "共18次检查"
        },
        recent_exams: [
          {
            exam_type: "左足第一跖趾关节X光检查",
            date: "2024-01-14 10:30",
            doctor: "赵医生",
            department: "放射科",
            status: "发现结石"
          },
          {
            exam_type: "左足关节B超检查",
            date: "2024-01-12 09:15",
            doctor: "孙医生",
            department: "超声科",
            status: "正常"
          }
        ],
        status_counts: {
          completed: 1,
          pending: 1,
          abnormal: 1
        }
      }
    },
    {
      patient_id: "PT2025003",
      name: "徐雄",
      imaging_exams: {
        summary: {
          CT: "共9次检查",
          MRI: "共6次检查",
          Ultrasound: "共17次检查",
          X_ray: "共22次检查"
        },
        recent_exams: [
          {
            exam_type: "双足X光检查",
            date: "2024-01-16 11:30",
            doctor: "周医生",
            department: "放射科",
            status: "发现结石"
          },
          {
            exam_type: "双足关节B超检查",
            date: "2024-01-14 11:15",
            doctor: "郑医生",
            department: "超声科",
            status: "正常"
          }
        ],
        status_counts: {
          completed: 2,
          pending: 1,
          abnormal: 0
        }
      }
    },
    {
      patient_id: "PT2025004",
      name: "涛林",
      imaging_exams: {
        summary: {
          CT: "共6次检查",
          MRI: "共4次检查",
          Ultrasound: "共11次检查",
          X_ray: "共16次检查"
        },
        recent_exams: [
          {
            exam_type: "右踝关节X光检查",
            date: "2024-01-13 08:30",
            doctor: "冯医生",
            department: "放射科",
            status: "正常无异常"
          },
          {
            exam_type: "右踝关节B超检查",
            date: "2024-01-11 10:15",
            doctor: "褚医生",
            department: "超声科",
            status: "发现结石"
          }
        ],
        status_counts: {
          completed: 2,
          pending: 0,
          abnormal: 1
        }
      }
    }
  ],
};
/**
 * 获取所有患者数据
 */
export function getAllPatients() {
  return imagingExamData.patients;
}

// 根据ID获取单个患者数据
export const getPatientById = (id: string): Patient | undefined => {
  return imagingExamData.patients.find(patient => patient.patient_id === id);
};


/**
 * 根据姓名查找患者
 * @param name 患者姓名
 */
export function getPatientByName(name: string) {
  return imagingExamData.patients.find(p => p.name === name);
}

/**
 * 获取患者的最近检查
 * @param name 患者姓名
 * @param count 要获取的最近检查数量(默认全部)
 */
export function getRecentExams(name: string, count?: number) {
  const patient = getPatientByName(name);
  if (!patient) return [];

  return count
    ? patient.imaging_exams.recent_exams.slice(0, count)
    : patient.imaging_exams.recent_exams;
}

/**
 * 按检查类型筛选检查记录
 * @param examType 检查类型(如"X光"、"B超")
 */
export function getExamsByType(examType: string) {
  const allExams: ImagingExam[] = [];
  imagingExamData.patients.forEach(patient => {
    patient.imaging_exams.recent_exams.forEach(exam => {
      if (exam.exam_type.includes(examType)) {
        allExams.push(exam);
      }
    });
  });
  return allExams;
}

/**
 * 按状态筛选患者
 * @param status 检查状态
 */
export function getPatientsByExamStatus(status: keyof ExamStatusCounts){
  return imagingExamData.patients.filter(
    patient => patient.imaging_exams.status_counts[status] > 0
  );
}

/**
 * 获取检查数量统计
 */
export function getExamStatistics(): {
  totalPatients: number;
  totalExams: number;
  byType: Record<string, number>;
} {
  const result = {
    totalPatients: imagingExamData.patients.length,
    totalExams: 0,
    byType: {
      CT: 0,
      MRI: 0,
      Ultrasound: 0,
      X_ray: 0
    }
  };

  imagingExamData.patients.forEach(patient => {
    result.byType.CT += parseInt(patient.imaging_exams.summary.CT.replace("共", "").replace("次检查", ""));
    result.byType.MRI += parseInt(patient.imaging_exams.summary.MRI.replace("共", "").replace("次检查", ""));
    result.byType.Ultrasound += parseInt(patient.imaging_exams.summary.Ultrasound.replace("共", "").replace("次检查", ""));
    result.byType.X_ray += parseInt(patient.imaging_exams.summary.X_ray.replace("共", "").replace("次检查", ""));
  });

  result.totalExams = Object.values(result.byType).reduce((sum, count) => sum + count, 0);

  return result;
}
